<?php
/**
 * Easthv Zend Framework Project
 *
 *
 * LICENSE:
 *
 * @category   Zend
 * @package    Zend_Helper
 * @copyright  Copyright (c) 2009-2010 Easthv Team @ East Hui Van Business Consulting Co., Ltd.(http://www.easthv.com)
 * @license    http://framework.zend.com/license/   BSD License
 * @version    1.0
 * @link       http://www.easthv.com
 * @since      1.0
*/

/**
 * Format Date time
 *
 * @category   Zend
 * @package    Zend_Helper
 * @copyright  Copyright (c) 2009-2010 Easthv Team @ East Hui Van Business Consulting Co., Ltd.(http://www.easthv.com)
 * @license    http://framework.zend.com/license/   BSD License
 * @version    1.0
 * @link       http://www.easthv.com
 * @since      1.0
 * @deprecated
 */
class Zend_View_Helper_DateTime
{
    /**
     * @var object Zend_View
     */
    public $view;

    /**
     * @var string Default ui time format
     */
    private $_formatDefault = '';


    /**
     * @var int timezone
     */
    private $_timezone = 0;

    /**
     * Set Zend View
     *
     *
     * @param  Zend_View_Interface   $view   zend view interface 
     *
     * @access public
     *
     * @return void
     */
    public function setView(Zend_View_Interface $view)
    {
        $this->view = $view;

        $this->_formatDefault = $this->view->config['date']['format']['default'];
        $this->_timezone = $this->view->config['date']['timezone'];
        date_default_timezone_set('UTC');

    }

    /**
     * Set Timezone
     *
     *
     * @param Int $timezone
     *
     * @access public
     *
     * @return void
     */
    public function setTimezone($timezone)
    {

        $this->_timezone = $timezone;
    
    }


    /**
     * DateTime Helper core function
     *
     * @param string $time
     * @param int $timezone
     * @param string $format
     *
     * @access public
     *
     * @return mixed     if there is input, return DateTime string with default format
     *                   if no input, return Zend_View_Helper object
     */
    public function DateTime($time = '', $timezone = null, $format = '')
    {
        if (0 == func_num_args()) {
            return $this;
        }

        $timezone = $timezone !== null ? $timezone : $this->_timezone;
        $format = $format ? $format : $this->_formatDefault;

        $time = $time ? $time : time();
        $time = is_numeric($time) ? $time : strtotime($time);
        $time = $time + $timezone * 3600;

        return date($format, $time);
    }

    /**
     * Tranform input time to timeStamp
     *
     * @param string $time
     *
     * @access public
     *
     * @return Int  timeStamp
     */
    public function timeStamp($time = '')
    {

        return $time ? strtotime($time) : time();
    
    }

    /**
     * Tranform input time to time string which can be parsed by javascript
     *
     * @param string $time
     *
     * @access public
     *
     * @return string javascript parse-able string
     */
    public function jsTime($time = '' , $timezone = null)
    {
        if (!$time) {
            return;
        }

        $timezone = $timezone ? $timezone : $this->_timezone;
    
        $time = $time ? strtotime($time) : time();
        $time = $time + $timezone * 3600;
        $prefix = $timezone < 0 ? '-' : '+';
    
        $zone = str_pad(str_pad(abs($timezone),2,0,STR_PAD_LEFT),4,0);
        return date('D M j H:i:s',$time) . ' UTC' . $prefix . $zone . ' ' . date('Y',$time);
    }

    /**
     * Tranform input time to time array
     *
     * @param string $time
     *
     * @access public
     *
     * @return array time array
     */
    public function arrayTime($time, $timezone = null)
    {
        if (!$time) {
            return array();
        }

        $timezone = $timezone ? $timezone : $this->_timezone;
        $time = strtotime($time) + $timezone * 3600;
        return array(
            'year' => gmdate("Y",$time),
            'month' => gmdate("M",$time),
            'day' => gmdate("d",$time),
            'hour' => gmdate("H",$time),
            'minute' => gmdate("i",$time),
            'second' => gmdate("s",$time),
        );
    }

    /**
     * Tranform input time to year,month,day
     *
     * @param string $time
     *
     * @access public
     *
     * @return array time array
     */
    public function parseDate($time, $format = 'year')
    {
        if (!$time) {
            return;
        }
        
        $time = strtotime($time);
        
        if ($format == 'year') {
            return date('Y',$time);
        } else if ($format == 'month') {
            return date('m',$time);
        } else if ($format == 'day') {
            return date('d',$time);
        } else {
            return;
        }
    }
   
    /**
     * Tranform input time to iso time
     *
     * @param string $time
     * @param int $timezone
     *
     * @access public
     *
     * @return string time string
     */
    public function isoTime($time = null, $timezone = null)
    {
        $timezone = $timezone ? $timezone : $this->_timezone;
		
		$time = $time ? strtotime($time) : time();
		return $time = date('c',$time);



		/*
        if ($timezone > 0) {
            $timezone = '+' . str_pad( abs($timezone) * 100,4,0,STR_PAD_LEFT);
        } else if ($timezone < 0) {
            $timezone = '-' . str_pad( abs($timezone) * 100,4,0,STR_PAD_LEFT);
        } else {
            $timezone = 'Z';
        }

		return str_replace(' ','T',$time) . $timezone;
		 */	
    }
    
    /**
     * Get a number of days for future date
     *
     * @param string $date
     *
     * @access public
     *
     * @return string 
     */
    public function preDate($date)
    {
        if (!$date) {
            return;
        }

        $diff = strtotime($date) - mktime();

        $days = ceil($diff / 86400);
        return $days > 1 ? "$days days" : "$days day";
    }
    
    /**
     * Get a number of days for past date
     * 
     *
     * @param string $date
     *
     * @access public
     *
     * @return string 
     */
    public function agoDate($date)
    {
        if (!$date) {
            return;
        }

        $diff = mktime() - strtotime($date);

        $days = ceil($diff / 86400);
        return $days > 1 ? "$days days" : "$days day";
    }

    /**
     * Tranform input time to human time
     *
     * @param string $time
     *
     * @access public
     *
     * @return string javascript parse-able string
     */
    public function agoTime($time)
    {
        if(!$time) {
            return;
        }

        //return $time;
        
        $diff = mktime() - strtotime($time);

        if ($diff < 0) {
            return $this->view->I18N('in the future');
        } else if ( $diff < 60 ) {
            return $this->view->I18N('less than a minute ago');
        } else if ( $diff < 120) {
            return $this->view->I18N('about a minute ago');
        } else if ( $diff < 3300) {
            //less than 55 mins
            return ceil($diff / 60) . $this->view->I18N(' minutes ago');
        } else if ( $diff < 3900) {
            //less than 65 mins
            return $this->view->I18N('about an hour ago');
        } else if ( $diff < 82800) {
            //less than 23 hours
            return floor($diff / 3600) . $this->view->I18N(' hours and ')  . ceil($diff % 3600 / 60) . $this->view->I18N(' minutes ago');
        } else if ( $diff < 90000) {
            //less than 25 hours
            return $this->view->I18N('about a day ago');
        } else if ( $diff < 518400) {
            //less than 6 days
            return floor($diff / 86400) . $this->view->I18N(' days ago');
        } else if ( $diff < 691200) {
            //less than 8 days
            return $this->view->I18N('about a week ago');
        } else if ( $diff < 2332800) {
            //less than 27 days
            return floor($diff / 86400) . $this->view->I18N(' days ago');
        } else if ( $diff < 3024000) {
            //less than 35 days
            return $this->view->I18N('about a month ago');
        } else if ( $diff < 28512000) {
            //less than 11 months 11 * 30 * 24 * 3 600 = 28 512 000
            return floor($diff / 2592000) . $this->view->I18N(' months ago');
        } else if ( $diff < 63072000) {
            //less than 2 years 13 * 30 * 24 * 3 600 = 33 696 000
            return $this->view->I18N('about a year ago');
        } else {
            //more than 24 months
            return ceil($diff / 31536000). $this->view->I18N(' years ago');
        }

    }
}
